<template>
	<view style="background-color: #F1F1F1;">
		<view class="news">
			<view class="title">
				——&nbsp;&nbsp;公司新闻&nbsp;&nbsp;——
			</view>
			<view class="content">
				<block v-for="(v, index) in news.list" :key="index">
					<view class="block flex" @tap="goDetail(v)">
						<view class="img"><image class="img" :src="v.img_url" mode="scaleToFill"></image></view>
						<view class="text">
							<text class="text1">{{v.title}}</text>
							<text class="text2">{{v.create_time}}</text>
						</view>
					</view>
				</block>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				news: {
					pageNumber: 1,
					firstPage: true,
					lastPage: true,
					list: [],
				},
			}
		},
		onReachBottom(){
			console.log('触底');
			if(this.page.lastPage) {this.noMore = true;return;}
			this.getNewsMore(++this.page.pageNumber);
		},
		methods: {
			getNews(pageNumber){
				let self = this;
				uni.showLoading({title: '加载中',mask: true});
				this.config.post('/api/news/findPage', {
					type: 2,
					pageNum: pageNumber?pageNumber:1,
				}).then(res=>{
					uni.hideLoading();
					if(res.data.code==='0'){
						self.news.pageNumber = res.data.page.pageNumber;
						self.news.firstPage = res.data.page.firstPage;
						self.news.lastPage = res.data.page.lastPage;
						self.news.list = res.data.page.list;
					}else{
						uni.showToast({title: self.config.handleError(res.data),icon: 'none'});
					}
				})
			},
			getNewsMore(pageNumber){
				let self = this;
				uni.showLoading({title: '加载中',mask: true});
				this.config.post('/api/news/findPage', {
					pageNum: pageNumber?pageNumber:1,
					type: 2,
				}).then(res=>{
					uni.hideLoading();
					if(res.data.code==='0'){
						self.news.pageNumber = res.data.page.pageNumber;
						self.news.firstPage = res.data.page.firstPage;
						self.news.lastPage = res.data.page.lastPage;
						self.news.list = self.news.list.concat(res.data.page.list);
					}else{
						uni.showToast({title: self.config.handleError(res.data),icon: 'none'});
					}
				})
			},
			goDetail(v){
				uni.navigateTo({
					url: '/pages/main/news/news-detail?id='+v.id
				})
			},
		},
		onShow() {
			this.getNews('1');
		},
	}
</script>

<style scoped lang="less">
	.news{
		width: 100%;
		margin-top: 10upx;
		background-color: #fff;
		text-align: center;
		.title{
			width: 100%;
			font-size: 25upx;
			font-weight: bold;
			color: #007aff;
			padding-top: 20upx;
			margin-bottom: 20upx;
		}
		.content{
			width: 100%;
			display: flex;
			flex-direction: column;
			text-align: center;
			margin-bottom: 10upx;
			.block{
				margin: 15upx 2% 15upx 2%;
				border-radius: 10upx;
				overflow: hidden;
				box-shadow: 0 0 20px 0 #ccc;
				.img{
					width: 180upx;
					height: 140upx;
					margin-right: 10upx;
				}
				.text{
					padding: 10upx;font-size: 25upx;text-align: left;
					display: flex;flex-direction: column;
					justify-content: space-between;align-items: flex-start;
					.text1{
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
					}
					.text2{
						color: #888;
					}
				}
			}
		}
	}
</style>
